//
//  ViewController.swift
//  xos-demo
//
//  Created by V on 2018/6/19.
//  Copyright © 2018年 V. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {

    let Default_Duration:TimeInterval = 6*60//6分钟
    
    @IBOutlet weak var timeLeftField: NSTextField!
    @IBOutlet weak var eggImageView: NSImageView!
    @IBOutlet weak var btnStart: NSButton!
    @IBOutlet weak var btnStop: NSButton!
    @IBOutlet weak var btnRestart: NSButton!
    
    var eggTimer = EggTimer()
    
    @IBAction func onStartClick(_ sender: Any) {
        if eggTimer.isPaused {
            eggTimer.resumeTimer()
        } else {
            eggTimer.duration = Default_Duration
            eggTimer.startTimer()
        }
    }
    
    @IBAction func onStopBtnClick(_ sender: Any) {
        eggTimer.stopTimer()
    }
    
    @IBAction func onRestartBtnClick(_ sender: Any) {
        eggTimer.resetTimer()
        updateDisplay(for: eggTimer.duration)
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        eggTimer.delegate = self
    }

    
}

//为协议方法创建一个单独的扩展，会使代码更加整洁
extension ViewController: EggTimerProtocol {
    
    func timeRemainingOnTimer(_ timer: EggTimer, timeRemaining: TimeInterval) {
        updateDisplay(for: timeRemaining)
    }
    
    func timerHasFinished(_ timer: EggTimer) {
        updateDisplay(for: 0)
    }
}

extension ViewController {
    
    // MARK: - Display
    
    func updateDisplay(for timeRemaining: TimeInterval) {
        timeLeftField.stringValue = textToDisplay(for: timeRemaining)
        eggImageView.image = imageToDisplay(for: timeRemaining)
    }
    
    private func textToDisplay(for timeRemaining: TimeInterval) -> String {
        if timeRemaining == 0 {
            return "Done!"
        }
        
        let minutesRemaining = floor(timeRemaining / 60)
        let secondsRemaining = timeRemaining - (minutesRemaining * 60)
        
        let secondsDisplay = String(format: "%02d", Int(secondsRemaining))
        let timeRemainingDisplay = "\(Int(minutesRemaining)):\(secondsDisplay)"
        
        return timeRemainingDisplay
    }
    
    private func imageToDisplay(for timeRemaining: TimeInterval) -> NSImage? {
        let percentageComplete = 100 - (timeRemaining / eggTimer.duration * 100)
        
        if eggTimer.isStopped {
            let stoppedImageName = (timeRemaining == 0) ? NSImage.Name("100") : NSImage.Name("stopped")
            
            return NSImage(named: stoppedImageName)
        }
        
        let imageName: NSImage.Name
        switch percentageComplete {
        case 0 ..< 25:
            imageName = NSImage.Name("0")
        case 25 ..< 50:
            imageName = NSImage.Name("25")
        case 50 ..< 75:
            imageName = NSImage.Name("50")
        case 75 ..< 100:
            imageName = NSImage.Name("75")
        default:
            imageName = NSImage.Name("100")
        }
        
        return NSImage(named: imageName)
    }
    
}






























